package com.borrowed.book.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.borrowed.book.system.domain.*;
import com.borrowed.book.system.exception.ReDictKeyException;
import com.borrowed.book.system.exception.RecodeDictException;
import com.borrowed.book.system.mapper.SysDictMapper;
import com.borrowed.book.system.mapper.SysDictTypeMapper;
import com.borrowed.book.system.mapper.SystemMapper;
import com.borrowed.book.system.service.ISysDictService;
import com.borrowed.book.system.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ISysDictServiceImp implements ISysDictService {
    @Autowired
    private SystemMapper systemMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private SysDictMapper dictMapper;

    private final String DICTIONARIES_KEY="basic:dictionaries";

    @Override
    public String transformStr(String distname, int key) {
        return null;
    }

    /**
     * 刷新字典缓存
     */
    @Override
    public Map getDictMap() {
        List<SysDictTypePojo> dictTypeList=systemMapper.getAllDictType();
        List<SysDictPojo>  dictList=systemMapper.getAllDict();
        Map<String, String> dictionariesMap = new TreeMap<>();

        for(SysDictTypePojo type:dictTypeList){
            List<SysDictPojo> data=new ArrayList<>();
            for(SysDictPojo  dict:dictList){
                if(type.getCode().equals(dict.getTypeCode())){
                    data.add(dict);
                }
            }
            dictionariesMap.put(type.getCode(), JSONObject.toJSON(data).toString());
            type.setData(data);
        }
        return dictionariesMap;
    }

    @Override
    public List<SysDictPojo> getDictByType(String code){
       Map map= getDictMap();
       String jsonStringDict= (String) map.get(code);
       List<SysDictPojo> dictList=JSONObject.parseArray(jsonStringDict,SysDictPojo.class);
       return dictList;
    }

    @Override
    public SysDictPojo getDictByTypeAndKey(String type, String key) {
        Map<String,Object> map=new HashMap<>();
        map.put("type",type);
        map.put("key",key);
        SysDictPojo dictPojo=dictMapper.selectDictByTypeAndKey(map);
        return dictPojo;
    }

    @Override
    public List<SysDictTypePojo> getAllDictList(){
        Map map= getDictMap();
        List<SysDictTypePojo> allDictList=new ArrayList<>();
        for (Object key : map.keySet()) {
            SysDictTypePojo  dict=new SysDictTypePojo();
            String jsonStringDict = (String) map.get((String)key);
            List<SysDictPojo> dictList=JSONObject.parseArray(jsonStringDict,SysDictPojo.class);
            dict.setCode((String)key);
            dict.setData(dictList);
            allDictList.add(dict);
        }
        return allDictList;
    }

    @Override
    public List<SysDictType> selectDictTypeList(SelectDictTypeInDto param) {
        Map map= BeanUtils.getObjectToMap(param);
        List<SysDictType>  typeList=dictTypeMapper.getDictTypeByParam(map);
        return typeList;
    }

    //新增数据字典
    @Override
    public void addDictType(SysDictType param) throws RecodeDictException {
        Map map=BeanUtils.getObjectToMap(param);
        //检测字典code是否重复
        SysDictType dictType=dictTypeMapper.selectDictTypeByCode(param.getCode());
        if(dictType!=null){
            throw new RecodeDictException();
        }else{
            dictTypeMapper.insert(param);
        }
    }
    //根据字典类型查询字典属性
    @Override
    public List<SysDict> selectDictByType(String code) {
        List<SysDict>  list=dictMapper.selectDictByType(code);
        return list;
    }

    //添加字典属性
    @Override
    public void addDict(SysDict param) throws ReDictKeyException {
        Map map=BeanUtils.getObjectToMap(param);
        //检查该类型字典的属性key是否重复
        SysDict dict=dictMapper.selectDictByKey(map);
        if(dict!=null){
            throw new ReDictKeyException();
        }else{
            dictMapper.insert(param);
        }
    }

    @Override
    public void deleteDictById(Long id) {
        dictMapper.deleteById(id);
    }

    @Override
    public void updateDict(SysDict param) throws ReDictKeyException {
        Map map=BeanUtils.getObjectToMap(param);
        //检查该类型字典的属性key是否重复
        SysDict dict=dictMapper.selectDictByKey(map);
        if(dict!=null){
            throw new ReDictKeyException();
        }else {
            dictMapper.updateDict(map);
        }
    }

    @Override
    public void deleteDictTypeByCode(String code) {
        dictTypeMapper.deleteDictTypeByCode(code);
        dictMapper.deleteByTypeCode(code);
    }
}
